package edu.pw.p2pclient.networking.webservice;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

public class ResourcesResponse extends Response {
	List<FoundResource> mRequests = null;

	public ResourcesResponse(int exitCode, String message,
			FoundResource[] results) {
		super(exitCode, message);
		mRequests = new ArrayList<FoundResource>();
		for (FoundResource res : results)
			mRequests.add(res);
	}

	public List<FoundResource> getResults() {
		return mRequests;
	}

	public final static JsonDeserializer<ResourcesResponse> deserializer = new JsonDeserializer<ResourcesResponse>() {

		@Override
		public ResourcesResponse deserialize(JsonElement json, Type typeOfT,
				JsonDeserializationContext context) throws JsonParseException {

			int exitCode = json.getAsJsonObject().get("exitCode").getAsInt();
			String message = json.getAsJsonObject().get("message")
					.getAsString();
			FoundResource[] results = context.deserialize(json
					.getAsJsonObject().get("result"), FoundResource[].class);

			return new ResourcesResponse(exitCode, message, results);
		}
	};
}
